package business_opportunity

import (
	"context"
	"git.mycaigou.com/gfyx/common/rpc_client"
	"git.mycaigou.com/gfyx/micro-gfyx-mp-api/infrastructure/common/constants"
	pb "git.mycaigou.com/gfyx/micro-gfyx-mp-api/proto/micro_gfyx_oppty_service_proto/micro-gfyx-oppty-service/business_opportunity"
	"git.myscrm.cn/golang/stark/v4"
)

type bidRepository struct {
}

// NewBidRepository
// 找招中标
func NewBidRepository() BidRepositoryIface {
	return &bidRepository{}
}

// SearchBid 搜索招中标
func (r *bidRepository) SearchBid(ctx context.Context, request *pb.SearchBidRequest) (*pb.SearchBidResponse, error) {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_OPPTY_SERVICE)
	if err != nil {
		stark.Logger.Errorf(ctx, "SearchBid request: %+v, err:%s", request, err.Error())
		return nil, err
	}
	defer func() {
		_ = conn.Close()
	}()

	client := pb.NewBidServiceClient(conn)
	res, err := client.SearchBid(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "SearchBid request: %+v, err:%s", request, err.Error())
		return nil, err
	}
	return res, nil
}

// FuzzySearchBid 模糊搜索招中标
func (r *bidRepository) FuzzySearchBid(ctx context.Context, request *pb.FuzzySearchBidRequest) (*pb.FuzzySearchBidResponse, error) {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_OPPTY_SERVICE)
	if err != nil {
		stark.Logger.Errorf(ctx, "FuzzySearchBid request: %+v, err:%s", request, err.Error())
		return nil, err
	}
	defer func() {
		_ = conn.Close()
	}()

	client := pb.NewBidServiceClient(conn)
	res, err := client.FuzzySearchBid(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "FuzzySearchBid request: %+v, err:%s", request, err.Error())
		return nil, err
	}
	return res, nil
}

// BidDetail 招标详情
func (r *bidRepository) BidDetail(ctx context.Context, request *pb.BidDetailRequest) (*pb.BidDetailResponse, error) {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_OPPTY_SERVICE)
	if err != nil {
		stark.Logger.Errorf(ctx, "BidDetail request: %+v, err:%s", request, err.Error())
		return nil, err
	}
	defer func() {
		_ = conn.Close()
	}()

	client := pb.NewBidServiceClient(conn)
	res, err := client.BidDetail(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "BidDetail request: %+v, err:%s", request, err.Error())
		return nil, err
	}
	return res, nil
}

// BidDetailMobile 招标详情-手机号明文
func (r *bidRepository) BidDetailMobile(ctx context.Context, request *pb.BidDetailMobileRequest) (*pb.BidDetailMobileResponse, error) {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_OPPTY_SERVICE)
	if err != nil {
		stark.Logger.Errorf(ctx, "BidDetailMobile request: %+v, err:%s", request, err.Error())
		return nil, err
	}
	defer func() {
		_ = conn.Close()
	}()

	client := pb.NewBidServiceClient(conn)
	res, err := client.BidDetailMobile(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "BidDetailMobile request: %+v, err:%s", request, err.Error())
		return nil, err
	}
	return res, nil
}

// WinBidDetail 中标详情
func (r *bidRepository) WinBidDetail(ctx context.Context, request *pb.WinBidDetailRequest) (*pb.WinBidDetailResponse, error) {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_OPPTY_SERVICE)
	if err != nil {
		stark.Logger.Errorf(ctx, "WinBidDetail request: %+v, err:%s", request, err.Error())
		return nil, err
	}
	defer func() {
		_ = conn.Close()
	}()

	client := pb.NewBidServiceClient(conn)
	res, err := client.WinBidDetail(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "WinBidDetail request: %+v, err:%s", request, err.Error())
		return nil, err
	}
	return res, nil
}
